package com.kingyea.mobilepolice.menu.service.impl;

import com.google.common.collect.Lists;
import com.kingyea.mobilepolice.menu.bean.Menu;
import com.kingyea.mobilepolice.menu.bean.MenuCondition;
import com.kingyea.mobilepolice.menu.dao.MenuMapper;
import com.kingyea.mobilepolice.menu.service.MenuService;
import com.kingyea.mobilepolice.usermenu.bean.UserMenu;
import com.kingyea.mobilepolice.usermenu.bean.UserMenuCondition;
import com.kingyea.mobilepolice.usermenu.dao.UserMenuMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hx
 * @version v1.0.0
 * @description Menu 实现类
 * @date Created in 2018-04-17 15:13:38
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MenuServiceImpl.class);

    @Resource
    private MenuMapper menuMapper;
    @Resource
    private UserMenuMapper userMenuMapper;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public Menu getById(Integer id) throws Exception {
        return this.menuMapper.getById(id);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<Menu> search(MenuCondition condition) throws Exception {
        List<Menu> list = menuMapper.search(condition);
        return list;
    }

    @Override
    public List<Menu> searchByUserId(String userId, Integer isAdd) throws Exception {
        UserMenuCondition userMenuCondition = new UserMenuCondition();
        userMenuCondition.setPageAble(false);
        userMenuCondition.setUserId(userId);
        userMenuCondition.setIsAdd(isAdd);
        MenuCondition menuCondition = new MenuCondition();
        menuCondition.setPageAble(false);
        List<UserMenu> list = userMenuMapper.search(userMenuCondition);
        List<Menu> menuList = Lists.newArrayList();
        if (list.size() > 0) {
            menuCondition.setUserMenuList(list);
            menuList.addAll(menuMapper.search(menuCondition));
        }
        return menuList;
    }

    @Override
    public List<Menu> searchMenuTreeByuserId(String userId) throws Exception {
        UserMenuCondition userMenuCondition = new UserMenuCondition();
        userMenuCondition.setUserId(userId);
        userMenuCondition.setPageAble(false);
        List<UserMenu> list = userMenuMapper.search(userMenuCondition);
        List<Menu> menuList = Lists.newArrayList();
        if (list.size() > 0) {
            MenuCondition menuCondition = new MenuCondition();
            menuCondition.setPageAble(false);
            menuCondition.setUserMenuList(list);
            menuList.addAll(menuMapper.search(menuCondition));
        }
        return menuList;
//		Set<Integer> menuIdList = list.stream().map(bean -> bean.getMenuId()).collect(Collectors.toSet());
//		List<Menu> roleList = Lists.newArrayList();
//		if(menuIdList.size() > 0) {
//			MenuCondition menuCondition = new MenuCondition();
//			menuCondition.set
//			roleCondition.setRoleIds(Lists.newArrayList(roleIdList));
//			roleCondition.setPageAble(false);
//			roleList.addAll(roleMapper.search(roleCondition));
//		}
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) throws Exception {
        menuMapper.deleteById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param menu
     * @return
     */
    @Override
    public Menu save(Menu menu) throws Exception {
        try {
            menuMapper.save(menu);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return menu;
    }

    /**
     * 更新
     *
     * @param menu
     * @return
     */
    @Override
    public Menu update(Menu menu) throws Exception {
        try {
            menuMapper.update(menu);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return menu;
    }

}
